package cn.stylefeng.guns.sys.modular.dataManagement.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.dataManagement.entity.StandardManagement;
import cn.stylefeng.guns.sys.modular.dataManagement.mapper.StandardManagementMapper;
import cn.stylefeng.guns.sys.modular.dataManagement.model.params.StandardManagementParam;
import cn.stylefeng.guns.sys.modular.dataManagement.model.result.StandardManagementResult;
import cn.stylefeng.guns.sys.modular.dataManagement.service.StandardManagementService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 数据标准管理表 服务实现类
 * </p>
 *
 * @author author
 * @since 2020-11-16
 */
@Service
public class StandardManagementServiceImpl extends ServiceImpl<StandardManagementMapper, StandardManagement> implements StandardManagementService {

    @Override
    public void add(StandardManagementParam param) {
        StandardManagement entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StandardManagementParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(StandardManagementParam param) {
        StandardManagement oldEntity = getOldEntity(param);
        StandardManagement newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StandardManagementResult findBySpec(StandardManagementParam param) {
        return null;
    }

    @Override
    public List<StandardManagementResult> findListBySpec(StandardManagementParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(StandardManagementParam param) {
        param.setDataIsDelete(1);
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int deleteDataStandardInfo(Integer id) {
        return this.baseMapper.deleteDataStandard(id);
    }

    private Serializable getKey(StandardManagementParam param) {
        return param.getDataStandardId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StandardManagement getOldEntity(StandardManagementParam param) {
        return this.getById(getKey(param));
    }

    private StandardManagement getEntity(StandardManagementParam param) {
        StandardManagement entity = new StandardManagement();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
